<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-1
 * Time: 上午11:40
 */

namespace App\Repositories\Admin;

use App\Models;
use Illuminate\Support\Facades\Cache;

class ConfigRepository extends AdminRepository
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new Models\Admin\Config();
    }

    private static $instance;

    /**
     * 实现单例
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * 声明实例模型
     * @param array $filter
     * @return mixed
     */
    public function getSelectModel(array $filter = [])  : \Illuminate\Database\Eloquent\Builder
    {
        $model = $this->model;

        if (isset($filter['group']) && $filter['group'] !== null) {
            $model = $model->where('group', '=', $filter['group']);
        }

        if (isset($filter['type']) && $filter['type'] !== null) {
            $model = $model->where('type', '=', $filter['type']);
        }

        if (isset($filter['is_developer']) && $filter['is_developer'] !== null) {
            $model = $model->where('is_developer', '=', $filter['is_developer']);
        }

        $model = $model->where('deleted_at', null);

        return $model;
    }

    /**
     * 列表页面
     * @param null $group
     * @param null $isDeveloper
     * @param null $type
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getPaginateData($group = null, $isDeveloper = null, $type = null ): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return $this->getSelectModel([
            'group' => $group,
            'is_developer' => $isDeveloper,
            'type' => $type,
        ])->orderBy('sort','asc')->orderBy('id', 'asc')->paginate();
    }

    /**
     * 读取站点所有配置
     * @return array
     */
    public function getConfigList()
    {
        $result = $this->getSelectModel()->get()->toArray();
        $config = [];
        foreach ($result as $key=>$value) {
            $config[$value['name']] = $this->parse($value['type'], $value['value']);
        }
        return $config;
    }

    /**
     * 根据配置类型解析配置
     * @param  integer $type  配置类型
     * @param  string  $value 配置值
     * @author 鬼国二少 <guiguoershao@163.com>
     * @return array
     */
    private function parse($type, $value){
        switch ($type) {
            case 3: //解析数组
                $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
                if(strpos($value,'=>')){
                    $value  = array();
                    foreach ($array as $val) {
                        list($k, $v) = explode('=>', $val);
                        if(strpos($v,'|') !== false){
                            $value[$k]   = explode('|', $v);
                        } else {
                            $value[$k]   = $v;
                        }
                    }
                }else{
                    $value =    $array;
                }
                break;

            case 6:
                $value = json_decode($value, true);
                break;
        }
        return $value;
    }
}